<template>
  <div class="app-container">
    <div class="mainbox">
      <dt-table
        v-loading="loading"
        :table-opts="tableOpts"
        :pagination-data="{
          total:total,
          queryParams:queryParams
        }"
        :is-column-search="false"
        @pagination="pagination"
        @searchReset="handleReset"
        @searchChange="handleQuery"
      />
    </div>
    <generate-task ref="generateTaskRef" />
    <automatic-task ref="automaticTaskRef" />
    <task-detail ref="taskDetailRef" />
    <app-topic ref="appTopicRef" />
    <data-config ref="dataConfigRef" />
  </div>
</template>

<script>
import dataSynchronizationApi from '@/framework/api/datasync/dataSynchronization'
import generateTask from './components/generateTaskManage.vue'
import automaticTask from './components/automaticTaskManage.vue'
import taskDetail from './components/taskDetail.vue'
import appTopic from './components/appTopicManage.vue'
import dataConfig from './components/dataConfig'

export default {
  name: 'TaskManagement',
  dicts: ['dataType', 'operationType', 'generateType', 'status', 'syncStratey'],
  components: {
    generateTask,
    automaticTask,
    taskDetail,
    appTopic,
    dataConfig
  },
  data() {
    return {
      dataSyncAppsList: [],
      tableOpts: {
        configPagination: '1',
        firstTableCol: {
          select: false,
          width: 70
        },
        indexTableCol: {
          label: '序号',
          width: 70
        },
        tableData: [],
        toolbarBtn: [
          {
            label: '手动生成任务',
            type: 'primary',
            permission: ['TASKMANAGEMENT_GENERATETASK_BUTTON'],
            click: (type) => {
              this.createGenerateTask(type)
            }
          },
          {
            label: '配置自动任务策略',
            type: 'primary',
            permission: ['TASKMANAGEMENT_AUTOMATICTASK_BUTTON'],
            click: (type) => {
              this.createAutomaticTask(type)
            }
          },
          {
            label: '数据同步配置',
            type: 'primary',
            click: (type) => {
              this.createDataConfig(type)
            }
          }
          // {
          //   label: '配置应用TOPIC',
          //   type: 'primary',
          //   permission: ['TASKMANAGEMENT_APPTOPIC_BUTTON'],
          //   click: (type) => {
          //     this.createAppTopic(type)
          //   }
          // }
        ],
        tableCol: [
          {
            prop: 'jobName',
            label: '任务名称',
            show: true,
            search: true
          },
          {
            prop: 'jobId',
            label: '任务编码',
            show: true
          },
          {
            prop: 'generateType',
            label: '任务生成方式',
            show: true,
            type: 'dict',
            formatter: () => {
              return this.dict.type.generateType
            }
          },
          {
            prop: 'syncStratey',
            label: '推送方式',
            show: true,
            formatter: (row) => {
              return row.syncStratey === 1 ? 'MQ' : 'API'
            }
          },
          {
            prop: 'appName',
            label: '应用名称',
            show: true
          },
          {
            prop: 'appId',
            label: '应用名称',
            show: false,
            search: true,
            type: 'dict',
            selectValue: 'appId',
            selectLabel: 'appName',
            formatter: () => {
              return this.dataSyncAppsList
            }
          },
          {
            prop: 'dataType',
            label: '同步信息类型',
            show: true,
            type: 'dict',
            formatter: () => {
              return this.dict.type.dataType
            }
          },
          {
            prop: 'operationType',
            label: '推送类型',
            show: true,
            type: 'dict',
            formatter: () => {
              return this.dict.type.operationType
            }
          },
          {
            prop: 'syncCount',
            label: '推送数量',
            show: true
          },
          {
            prop: 'status',
            label: '推送状态',
            show: true,
            type: 'dict',
            formatter: () => {
              return this.dict.type.status
            }
          },
          {
            prop: 'createTime',
            label: '任务创建时间',
            show: true
          }
        ],
        operator: {
          width: 230,
          operatorBtn: [
            {
              text: '执行',
              permission: ['TASKMANAGEMENT_RUN_BUTTON'],
              size: 'small',
              click: (row, col, cellValue) => { return this.raskRun(row) },
              disabledChange: (row) => { return !(row.generateType == 1 && (row.status == 0 || row.status == 3)) }
            },
            {
              text: '详情',
              size: 'small',
              click: (row, col, cellValue) => { return this.handleDetail(row) }
            },
            {
              text: '删除',
              permission: ['TASKMANAGEMENT_DEL_BUTTON'],
              size: 'small',
              click: (row, col, cellValue) => { return this.handelDelete(row) },
              disabledChange: (row) => { return row.status != 0 }
            }
          ]
        }
      },
      loading: false,
      total: 0,
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        jobName: '',
        appId: ''
      }
    }
  },
  async created() {
    this.getList()
    await this.dataSyncApps()
  },
  methods: {
    // 获取页面列表
    getList() {
      this.loading = true
      dataSynchronizationApi.dataSyncJobPage(this.queryParams).then((res) => {
        this.tableOpts.tableData = res.data.rows
        this.total = res.data.totalRows
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery(formModel = {}) {
      this.queryParams = { ...this.queryParams, ...formModel }
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
        jobName: '',
        appId: ''
      }
      this.handleQuery()
    },
    // 查看详情
    handleDetail(row) {
      this.$refs.taskDetailRef.openDialog(row)
    },
    // 手动执行任务
    raskRun(row) {
      dataSynchronizationApi.executeJob({ jobId: row.jobId }).then((res) => {
        this.$dtMessage({
          title: '成功',
          message: '任务已执行，具体推送信息可点击详情查看',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },
    // 手动生成任务
    createGenerateTask() {
      this.$refs.generateTaskRef.openDialog()
    },
    // 配置自动任务策略
    createAutomaticTask() {
      this.$refs.automaticTaskRef.openDialog()
    },
    // 配置应用TOPIC
    createAppTopic() {
      this.$refs.appTopicRef.openDialog()
    },
    // 数据同步配置
    createDataConfig() {
      this.$refs.dataConfigRef.openDialog()
    },
    // 删除任务
    handelDelete(row) {
      this.$dtMessageBox({ title: '提示', message: `确定要删除“${row.jobName}”吗?`, type: 'warning' }).then(() => {
        dataSynchronizationApi.delete({ jobId: row.jobId }).then((res) => {
          this.getList()
          this.$dtMessage({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    pagination() {
      this.getList()
    },
    // 获取应用名称
    async  dataSyncApps() {
      const res = await dataSynchronizationApi.getStatusThirdAppList()
      this.dataSyncAppsList = res.data
    }
  }
}
</script>
